home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Linux / SLAX 6.0.8 / slax-6.0.8.iso / slax / base / 006-devel.lzm / usr / include / kio / tcpslavebase.h < prev    next >
Encoding:
C/C++ Source or Header  |  2005-10-10  |  11.3 KB  |  390 lines

  1. /*
  2.  * Copyright (C) 2000 Alex Zepeda <zipzippy@sonic.net>
  3.  * Copyright (C) 2001 George Staikos <staikos@kde.org>
  4.  * Copyright (C) 2001 Dawit Alemayehu <adawit@kde.org>
  5.  *
  6.  * This file is part of the KDE project
  7.  *
  8.  * This library is free software; you can redistribute it and/or
  9.  * modify it under the terms of the GNU Library General Public
  10.  * License as published by the Free Software Foundation; either
  11.  * version 2 of the License, or (at your option) any later version.
  12.  *
  13.  * This library is distributed in the hope that it will be useful,
  14.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  16.  * Library General Public License for more details.
  17.  *
  18.  * You should have received a copy of the GNU Library General Public License
  19.  * along with this library; see the file COPYING.LIB.  If not, write to
  20.  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  21.  * Boston, MA 02110-1301, USA.
  22.  */
  23.  
  24. #ifndef _TCP_SLAVEBASE_H
  25. #define _TCP_SLAVEBASE_H
  26.  
  27. #include <sys/types.h>
  28. #include <stdio.h>
  29.  
  30. #include <kextsock.h>
  31. #include <kio/slavebase.h>
  32.  
  33.  
  34. namespace KIO {
  35.  
  36. /**
  37.  * There are two classes that specifies the protocol between application (job)
  38.  * and kioslave. SlaveInterface is the class to use on the application end,
  39.  * SlaveBase is the one to use on the slave end.
  40.  *
  41.  * Slave implementations should simply inherit SlaveBase
  42.  *
  43.  * A call to foo() results in a call to slotFoo() on the other end.
  44.  */
  45. class KIO_EXPORT TCPSlaveBase : public SlaveBase
  46. {
  47. public:
  48.     TCPSlaveBase(unsigned short int defaultPort, const QCString &protocol,
  49.                  const QCString &poolSocket, const QCString &appSocket);
  50.  
  51.     TCPSlaveBase(unsigned short int defaultPort, const QCString &protocol,
  52.                  const QCString &poolSocket, const QCString &appSocket,
  53.                  bool useSSL);
  54.  
  55.     virtual ~TCPSlaveBase();
  56.  
  57. protected:
  58.  
  59. #ifndef KDE_NO_COMPAT
  60.     /**
  61.      * @deprecated Due to inconsistency with KDE naming convention.
  62.      */
  63.     KDE_DEPRECATED ssize_t Write(const void *data, ssize_t len) { return write( data, len ); }
  64.  
  65.     /**
  66.      * @deprecated Due to inconsistency with KDE naming convention.
  67.      */
  68.     KDE_DEPRECATED ssize_t Read(void *data, ssize_t len) { return read( data, len ); }
  69.  
  70.     /**
  71.      * @deprecated Due to inconsistency with KDE naming convention.
  72.      */
  73.     KDE_DEPRECATED ssize_t ReadLine(char *data, ssize_t len) { return readLine( data, len ); }
  74.  
  75.     /**
  76.      * @deprecated Due to inconsistency with KDE naming convention.
  77.      */
  78.     KDE_DEPRECATED unsigned short int GetPort(unsigned short int p) { return port(p); }
  79.  
  80.     /**
  81.      * @deprecated Due to inconsistency with KDE naming convention.
  82.      */
  83.     KDE_DEPRECATED bool ConnectToHost( const QString &host, unsigned int port,
  84.                         bool sendError ) { return connectToHost( host, port, sendError ); }
  85.  
  86.     /**
  87.      * @deprecated Due to inconsistency with KDE naming convention.
  88.      */
  89.     KDE_DEPRECATED void CloseDescriptor() { closeDescriptor(); }
  90.  
  91.     /**
  92.      * @deprecated Due to inconsistency with KDE naming convention.
  93.      */
  94.     KDE_DEPRECATED bool AtEOF() { return atEnd(); }
  95.  
  96.     /**
  97.      * @deprecated Due to inconsistency with KDE naming convention.
  98.      */
  99.     KDE_DEPRECATED bool InitializeSSL() { return initializeSSL(); }
  100.  
  101.     /**
  102.      * @deprecated Due to inconsistency with KDE naming convention.
  103.      */
  104.     KDE_DEPRECATED void CleanSSL() { cleanSSL(); }
  105. #endif
  106.  
  107.     /**
  108.      * This function acts like standard write function call
  109.      * except it is also capable of making SSL or SOCKS
  110.      * connections.
  111.      *
  112.      * @param data info to be sent to remote machine
  113.      * @param len the length of the data to be sent
  114.      *
  115.      * @return the actual size of the data that was sent
  116.      */
  117.     ssize_t write(const void *data, ssize_t len);
  118.  
  119.     /**
  120.      * This function acts like standard read function call
  121.      * except it is also capable of deciphering SSL data as
  122.      * well as handling data over SOCKSified connections.
  123.      *
  124.      * @param data storage for the info read from server
  125.      * @param len lenght of the info to read from the server
  126.      *
  127.      * @return the actual size of data that was obtained
  128.      */
  129.     ssize_t read(void *data, ssize_t len);
  130.  
  131.     /**
  132.      * Same as above except it reads data one line at a time.
  133.      */
  134.     ssize_t readLine(char *data, ssize_t len);
  135.  
  136.     /**
  137.      * Sets the maximum size of blocks read in during calls to readLine().
  138.      * This allows a slave to optimize for the protocol which it implements.
  139.      * Ideally this should be (common_line_length+1) or so.
  140.      * Making this too large will have adverse effects on performance.
  141.      * Initial/default value is 256(bytes)
  142.      */
  143.     void setBlockSize(int sz);
  144.  
  145.     /**
  146.      * Determines the appropriate port to use.
  147.      *
  148.      * This functions attempts to discover the appropriate port.
  149.      *
  150.      * @param _port the port to try, if it works, it is returned
  151.      * @return the default port if the given port doesn't work
  152.      */
  153.     unsigned short int port(unsigned short int _port);
  154.  
  155.     /**
  156.      * Performs the initial TCP connection stuff and/or
  157.      * SSL handshaking as necessary.
  158.      *
  159.      * Please note that unlike its deprecated counterpart, this
  160.      * function allows you to disable any error message from being
  161.      * sent back to the calling application!  You can then use the
  162.      * connectResult() function to determine the result of the
  163.      * request for connection.
  164.      *
  165.      * @param host hostname
  166.      * @param port port number to connect to
  167.      * @param sendError if true sends error message to calling app.
  168.      *
  169.      * @return on succes, true is returned.
  170.      *         on failure, false is returned and an appropriate
  171.      *         error message is send to the application.
  172.      */
  173.     bool connectToHost( const QString &host, unsigned int port,
  174.                         bool sendError = true );
  175.  
  176.     /**
  177.      * Are we using SSL?
  178.      *
  179.      * @return if so, true is returned.
  180.      *         if not, true isn't returned.
  181.      * @since 3.2
  182.      */
  183.     bool usingSSL() const { return m_bIsSSL; }
  184.  
  185.     /**
  186.      * Are we using TLS?
  187.      *
  188.      * @return if so, true is returned.
  189.      *         if not, true isn't returned.
  190.      * @since 3.2
  191.      */
  192.     bool usingTLS() const;
  193.  
  194.     /**
  195.      * @obsolete kept for binary compatibility
  196.      * Are we using TLS?
  197.      *
  198.      * @return if so, true is returned.
  199.      *         if not, true isn't returned.
  200.      */
  201.     bool usingTLS();
  202.  
  203.     /**
  204.      * Can we use TLS?
  205.      *
  206.      * @return if so, true is returned.
  207.      *         if not, true isn't returned.
  208.      */
  209.     bool canUseTLS();
  210.  
  211.     /**
  212.      * Start using TLS on the connection.
  213.      *
  214.      * @return on success, 1 is returned.
  215.      *         on failure, 0 is returned.
  216.      *         on TLS init failure, -1 is returned.
  217.      *         on connect failure, -2 is returned.
  218.      *         on certificate failure, -3 is returned.
  219.      */
  220.     int startTLS();
  221.  
  222.     /**
  223.      * Stop using TLS on the connection.
  224.      */
  225.     void stopTLS();
  226.  
  227.     /**
  228.      * Closes the current file descriptor.
  229.      *
  230.      * Call this function to properly close up the socket
  231.      * since it also takes care to prroperly close the stdio
  232.      * fstream stuff, as well as sets the socket back to -1
  233.      */
  234.     void closeDescriptor();
  235.  
  236.  
  237.     /**
  238.      * Returns true when end of data is reached
  239.      */
  240.     bool atEnd();
  241.  
  242.  
  243.     /**
  244.      * Call this if you use persistent connections and want all the
  245.      * metadata restored.  This is particularly important for SSL
  246.      * sessions since the app needs to know the state of connection,
  247.      * certificates, etc.
  248.      */
  249.     void setSSLMetaData();
  250.  
  251.  
  252.     /**
  253.      * Initializs all SSL variables
  254.      */
  255.     bool initializeSSL();
  256.  
  257.  
  258.     /**
  259.      * Cleans up all SSL settings.
  260.      */
  261.     void cleanSSL();
  262.  
  263.     /**
  264.      * Determines whether or not we are still connected
  265.      * to the remote machine.
  266.      *
  267.      * This method may fail to detect a closed SSL connection.
  268.      *
  269.      * return @p true if the socket is still active or
  270.      *           false otherwise.
  271.      */
  272.     bool isConnectionValid();
  273.  
  274.     /**
  275.      * Returns the status of the connection.
  276.      *
  277.      * This function allows you to invoke ConnectToHost
  278.      * with the @p sendError flag set to false so that you
  279.      * can send the appropriate error message back to the
  280.      * calling io-slave.
  281.      *
  282.      * @return the status code as returned by KExtendedSocket.
  283.      */
  284.     int connectResult();
  285.  
  286.     /**
  287.      * Wait for some type of activity on the socket
  288.      * for the period specified by @p t.
  289.      *
  290.      * @param t  length of time in seconds that we should monitor the
  291.      *           socket before timing out.
  292.      *
  293.      * @return true if any activity was seen on the socket before the
  294.      *              timeout value was reached, false otherwise.
  295.      */
  296.     bool waitForResponse( int t );
  297.  
  298.     /**
  299.      * Sets the mode of the connection to blocking or non-blocking.
  300.      *
  301.      * Be sure to call this function before calling connectToHost.
  302.      * Otherwise, this setting will not have any effect until the next
  303.      * @p connectToHost.
  304.      *
  305.      * @param b true to make the connection a blocking one, false otherwise.
  306.      */
  307.     void setBlockConnection( bool b );
  308.  
  309.     /**
  310.      * Sets how long to wait for orignally connecting to
  311.      * the requested before timinig out.
  312.      *
  313.      * Be sure to call this function before calling ConnectToHost,
  314.      * otherwise the setting will not take effect until the next call
  315.      * to @p ConnectToHost.
  316.      *
  317.      * @param t timeout value
  318.      */
  319.     void setConnectTimeout( int t );
  320.  
  321.     /**
  322.      * Returns true if SSL tunneling is enabled.
  323.      *
  324.      * @see setEnableSSlTunnel
  325.      */
  326.     bool isSSLTunnelEnabled();
  327.  
  328.     /**
  329.      * Set up SSL tunneling mode.
  330.      *
  331.      * Calling this function with a @p true argument will allow
  332.      * you to temprarly ignore the @p m_bIsSSL flag setting and
  333.      * make a non-SSL connection.  It is mostly useful for making
  334.      * connections to SSL sites through a non-transparent proxy
  335.      * server (i.e. most proxy servers out there).
  336.      *
  337.      * Note that once you have successfully "tunneled" through the
  338.      * proxy server you must call this function with its argument
  339.      * set to false to properly connect to the SSL site.
  340.      *
  341.      * @param enable if true SSL Tunneling will be enabled
  342.      */
  343.     void setEnableSSLTunnel( bool enable );
  344.  
  345.     /**
  346.      * Sets up the the real hostname for an SSL connection
  347.      * that goes through a proxy server.
  348.      *
  349.      * This function is essential in making sure that the
  350.      * real hostname is used for validating certificates from
  351.      * SSL sites!
  352.      *
  353.      * @param realHost the actual host name we are connecting to
  354.      */
  355.     void setRealHost( const QString& realHost );
  356.  
  357.     // don't use me!
  358.     void doConstructorStuff();
  359.  
  360.     // For the certificate verification code
  361.     int verifyCertificate();
  362.  
  363.     // For prompting for the certificate to use
  364.     void certificatePrompt();
  365.  
  366.     // Did the user abort (as the reason for connectToHost returning false)
  367.     bool userAborted() const;
  368.  
  369. protected:
  370.     int m_iSock;
  371.     bool m_bIsSSL;
  372.     unsigned short int m_iPort;
  373.     unsigned short int m_iDefaultPort;
  374.     QCString m_sServiceName;
  375.     FILE *fp;
  376.  
  377. private:
  378.     bool doSSLHandShake( bool sendError );
  379.  
  380. protected:
  381.     virtual void virtual_hook( int id, void* data );
  382. private:
  383.     class TcpSlaveBasePrivate;
  384.     TcpSlaveBasePrivate *d;
  385. };
  386.  
  387. }
  388.  
  389. #endif
  390.